﻿using DevExpress.XtraEditors;
using DXApplicationTangche.DTO;
using DXApplicationTangche.service;
using DXApplicationTangche.UC.订单一览新.辅料.service;
using DXApplicationTangche.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.订单一览新.辅料.辅料重构
{
    public partial class Form辅料图片 : DevExpress.XtraEditors.XtraForm
    {
        private String subMaterialCode;
        public Form辅料图片(String subMaterialCode,String subMaterialName)
        {
            InitializeComponent();
            this.subMaterialCode = subMaterialCode;
            this.textBox辅料.Text = subMaterialCode + " " + subMaterialName;
            //  显示
            try
            {
                this.pictureBox辅料图.Image = FileService.GetImageByUrl(URL.图片url + URL.辅料图片路径 + this.subMaterialCode + ".png");
            }
            catch { }
        }

        private void pictureBox辅料图_DoubleClick(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.subMaterialCode))
            {
                return;
            }
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "gif,jpg,png|*.gif;*.jpg;*.png;*.jpeg";
            ofd.Title = "请选择图片文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    //  转换图片格式，不是png格式的都转成png
                    string fileName = ofd.FileName;
                    if (!Path.GetExtension(fileName).Equals(".png"))
                    {
                        Image image = Bitmap.FromFile(fileName);
                        //  转格式并保存本地文件:
                        string saveDirectoryPath = @"pic\";
                        String path = saveDirectoryPath + subMaterialCode + @".png";
                        image.Save(path, ImageFormat.Png);
                        //  上传辅料图
                        辅料Service.uploadAccessoryFileByCode(this.subMaterialCode, path);
                        //  删除本地文件
                        try { File.Delete(path); } catch { }
                        MessageBox.Show("操作完成");
                    }
                    else
                    {
                        //  上传辅料图
                        辅料Service.uploadAccessoryFileByCode(this.subMaterialCode, ofd.FileName);
                    }
                    //  显示
                    try
                    {
                        this.pictureBox辅料图.Image = FileService.GetImageByUrl(URL.图片url + URL.辅料图片路径 + this.subMaterialCode + ".png");
                    }
                    catch { }
                }
                catch { }
            }
        }
    }
}